package com.brkj.test;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.test.model.DS_Exam;
import com.brkj.util.ConstAnts;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseActivity;
import com.brkj.util.view.PullListView;
import com.brkj.util.view.WidgetAnim;
import com.dgl.sdk.util.DBStore;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by colin.ding on 2014/5/28.
 */
public class ExamSearchActivity extends BaseActivity {

    @ViewInject(id= R.id.examListView)
    PullListView examListView;
    @ViewInject(id=R.id.noData,click="click_noData")
    TextView noData;

    private List<DS_Exam> examList;
    protected ExamAdapter adapter;
    private EditText searchTag;
    private Button search;
    private List<DS_Exam> offlineExamList = new ArrayList<DS_Exam>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_exam_search);

        searchTag = (EditText) findViewById(R.id.inputText);
        search = (Button) findViewById(R.id.search);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getExam();
            }
        });

        setActivityTitle("搜索题库");
        setReturnBtn();

        examListView.hideFooterView();

        offlineExamList = DBStore.readAll(DS_Exam.class, ConstAnts.DB_TABLE_OFFLINE_QUES_LIST);

    }

    public void click_noData(View v){
        getExam();
    }

    private void getExam() {
        if(WidgetAnim.checkIfEmpty(this, searchTag)){
            return;
        }
        String tag = searchTag.getText().toString();

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetExamList.params.UserID, MyApplication.myUserID);
        params.put(HttpInterface.HIF_GetExamList.params.tkname, tag);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetExamList.address, params, new MyAjaxCallBack<Object>(this) {


            @Override
            public void onSuccess(Object t) {
                // TODO Auto-generated method stub
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Exam>>(){}.getType();
                examList = gson.fromJson((String)t, listType);

                if((examList != null) &&(examList.size()>0)){
                    adapter = new ExamAdapter(ExamSearchActivity.this, examList, false);
                    examListView.setAdapter(adapter);

                    examListView.setVisibility(View.VISIBLE);
                    noData.setVisibility(View.GONE);
                }else{
                    examListView.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                }
            }

        });

    }


    /**
     * 合并一些本地的数据
     * @param list
     */
    private void mergeData(List<DS_Exam> list) {

        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(DS_Exam offlineExam:offlineExamList){
            map.put(offlineExam.getExamPaperID(), offlineExam.getExamPaperID());
        }

        for(DS_Exam exam:list){
            if(map.containsKey(exam.getExamPaperID())){
                exam.setDownload(true);
            }
        }
    }

    @Override
    public void finish() {
        super.finish();
    }
}
